/* 
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2010. All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:  Use,
 * duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp.
 *
 * This code is released under the terms of the Eclipse Public License
 * version 1.0 which can be found at the top level of this project or
 * or at http://www.eclipse.org/org/documents/epl-v10.php
 */

package com.ibm.mining.model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class GraphVizGenerator {

	public GraphVizGenerator() {

	}

	public void OutputModel(Model model, OutputStream stream) {
		PrintStream out = new PrintStream(stream);

		out.println("digraph model {");
		out.println("overlap=false;\nsplines=true;");
		for (ModelClass mc : model.getModelClasses().values()) {
			out.print(mc.getName() + " [shape=record, label=\"{<" + mc.getName() + ">" + 
					mc.getName());
			for (Property prop : mc.getProperties()) {
				if (prop.getName().equals("merge_predecessor") ||
						prop.getName().equals("predecessor")) {
					continue;
				}
				out.print("|<" + prop.getName() + ">" + prop.getName());
			}
			out.println("}\"];");
			for (Property prop : mc.getProperties()) {
				if (prop.getName().equals("merge_predecessor") ||
						prop.getName().equals("predecessor")) {
					continue;
				}
				Type type = prop.getType();

				if (type.hasBaseModelClassType()) {
					String targetName = type.getBaseModelClassForType().getName();
					out.println(mc.getName() + ":" + prop.getName() + 
							" -> " + targetName +";");
				}
			}
		}
		out.println("}");
	}

	public static void main(String args[]) throws FileNotFoundException {
		new GraphVizGenerator().OutputModel(new JazzMiningModel(), new FileOutputStream("schema.dot"));
		new GraphVizGenerator().OutputModel(new JazzMiningModel(), System.out);
	}
}
